package io.geekidea.boot.jieanbo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.jieanbo.dto.AiSetDto;
import io.geekidea.boot.jieanbo.entity.AiSet;
import io.geekidea.boot.jieanbo.mapper.AiSetMapper;
import io.geekidea.boot.jieanbo.query.AiSetQuery;
import io.geekidea.boot.jieanbo.service.AiSetService;
import io.geekidea.boot.jieanbo.vo.AiSetVo;
import io.geekidea.boot.jieanbo.query.AppAiSetQuery;
import io.geekidea.boot.jieanbo.vo.AppAiSetVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * AI设置表 服务实现类
 *
 * @author geekidea
 * @since 2025-06-04
 */
@Slf4j
@Service
public class AiSetServiceImpl extends ServiceImpl<AiSetMapper, AiSet> implements AiSetService {

    @Autowired
    private AiSetMapper aiSetMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addAiSet(AiSetDto dto) {
        AiSet aiSet = new AiSet();
        BeanUtils.copyProperties(dto, aiSet);
        return save(aiSet);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateAiSet(AiSetDto dto) {
        Integer id = dto.getId();
        AiSet aiSet = getById(id);
        if (aiSet == null) {
            throw new BusinessException("AI设置表不存在");
        }
        BeanUtils.copyProperties(dto, aiSet);
        boolean result = updateById(aiSet);
        
        // Store in Redis
        if (result) {
            String redisKey = "jieanboAi";
            redisTemplate.opsForValue().set(redisKey, dto);
        }
        
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteAiSet(Integer id) {
        return removeById(id);
    }

    @Override
    public AiSetVo getAiSetById(Integer id) {
        return aiSetMapper.getAiSetById(id);
    }

    @Override
    public Paging<AiSetVo> getAiSetPage(AiSetQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AiSetVo> list = aiSetMapper.getAiSetPage(query);
        Paging<AiSetVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppAiSetVo getAppAiSetById(Integer id) {
        return aiSetMapper.getAppAiSetById(id);
    }

    @Override
    public Paging<AppAiSetVo> getAppAiSetPage(AppAiSetQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppAiSetVo> list = aiSetMapper.getAppAiSetPage(query);
        Paging<AppAiSetVo> paging = new Paging<>(list);
        return paging;
    }

    /**
     * 获取单条AI设置数据
     * @return AiSetVo
     */
    @Override
    public AiSetVo getOneAiSet() {
        return aiSetMapper.getOneAiSet();
    }

}
